<?php


namespace app\community\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Message extends Controller
{

    public function _initialize()
    {
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }

    //删除操作
    public function delMessage()
    {
        $common = new Common();
        try {

            $id = $_POST['id'];
            $navList = Db::name("community_message")->where(['id' => ['in', $id]])->field("id,title")->select();
            $navList = array_column($navList, "id");
            $navList = implode($navList, ",");

            Db::name("community_message")->where(['id' => ['in', $id]])->delete();

            $log['remark'] = "账号【" . Session::get("user_name") . "】删除内容【" . $navList . "】";
            $log['controller'] = "Message";
            $log['methods'] = "delMessage";
            $common->addLog($log);


            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //查看详情
    public function detail()
    {
        $id = $_POST['id'];
        $detail = Db::name("community_message")->where(['id' => $id])->find();
        $images = explode(",", $detail['image']);
        $this->assign("images", $images);
        $this->assign("detail", $detail);
        return view("detail");
    }

    //回复操作
    public function reply()
    {
        $common = new Common();
        try {
            $id = $_POST['id'];
            $content = $_POST['content'];
            $data['updatetime'] = time();
            $data['auto'] = Session::get("auto");
            $data['update_id'] = Session::get("id");
            $data['reply'] = $content;
            $data['is_reply'] = 2;
            Db::name("community_message")->where(['id' => $id])->update($data);

            $log['remark'] = "账号【" . Session::get("user_name") . "】执行回复操作,关联id【" . $id . "】";
            $log['controller'] = "Party";
            $log['methods'] = "delParty";
            $common->addLog($log);


            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //获取留言列表
    public function index()
    {
        $keyword = '';
        $name = '';
        $classify_id = '';
        $where = [];
        $is_reply = '';
        if (!empty($_GET['is_reply'])) {
            $is_reply = $_GET['is_reply'];
            $where['is_reply'] = $is_reply;
        }
        if (!empty($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $where['title'] = ['like', "%{$keyword}%"];
        }
        if (!empty($_GET['name'])) {
            $name = $_GET['name'];
            $where['name'] = ['like', "%{$name}%"];
        }
        if (!empty($_GET['classify_id'])) {
            $classify_id = $_GET['classify_id'];
            $where['classify_id'] = $classify_id;
        }


        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("community_message")->where($where)->order("id desc")->limit($start,$limit)->select();
                $reply = [1 => "未回复", 2 => "已回复"];
                foreach ($list as $k => $v) {
                    $list[$k]['is_reply'] = $reply[$v['is_reply']];
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = Db::name("community_message")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }


        $assign = [
            'keyword' => $keyword,
            'classify_id' => $classify_id,
            'name' => $name,
            'is_reply' => $is_reply
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

}